class HiDate {
  static int getCurrentMilliseconds() =>
      new DateTime.now().millisecondsSinceEpoch;

  static DateTime transformMilliseconds2DateTime(int timestamp) =>
      new DateTime.fromMillisecondsSinceEpoch(timestamp);

  static int parseAge(int timestamp) {
    return getAge(transformMilliseconds2DateTime(timestamp));
  }

  static int getAge(DateTime brt){
    int age = 0;
    DateTime dateTime = DateTime.now();
    if (dateTime.isBefore(brt)) { //出生日期晚于当前时间，无法计算
      return 0;
    }
    int yearNow = dateTime.year;  //当前年份
    int monthNow = dateTime.month;  //当前月份
    int dayOfMonthNow = dateTime.day; //当前日期

    int yearBirth = brt.year;
    int monthBirth = brt.month;
    int dayOfMonthBirth = brt.day;
    age = yearNow - yearBirth;   //计算整岁数
    if (monthNow <= monthBirth) {
      if (monthNow == monthBirth) {
        if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
      } else {
        age--;//当前月份在生日之前，年龄减一
      }
    }
    return age;
  }

  static String diffCurrent(dynamic dt) {
    Duration diff;
    if (dt is DateTime) {
      diff = new DateTime.now().difference(dt);
    } else if (dt is int) {
      diff = new DateTime.now().difference(transformMilliseconds2DateTime(dt));
    }
    if (diff.inMinutes < 60) {
      return "${diff.inMinutes}分钟前";
    } else if (diff.inHours < 24) {
      return "${diff.inHours}小时前";
    } else {
      return "${diff.inDays}天前";
    }
  }
}
